import $ from 'jquery';
import host from '../conf/hostConf';
const post = (params) => {
    if (!params.url) {
        console.log('url不可为空！');
        return;
    }
    let u = '';
    if (params.url.startsWith('/')) {
        u = params.url;
    }else{
        u = `/${params.url}`;
    }
    const opts = {
        method:'POST',
        xhrFields: {withCredentials: true},
        crossDomain: true,
        // dataType: 'json',
        // contentType: 'application/json',
        data:params.data
    }
    return $.ajax(`${host}${u}`,opts);
};

const get = (params) => {
    if (!params.url) {
        console.log('url不可为空！');
        return;
    }
    let u = '';
    if (params.url.startsWith('/')) {
        u = params.url;
    }else{
        u = `/${params.url}`;
    }
    let data = ''
    if (params.data) {
        let d = params.data;
        let ks = Object.keys(d);
        for (let i = 0;i < ks.length;i++) {
            data += `${ks[i]}=${d[ks[i]]}&`;
        }
    }
    data = data.substring(0,data.length-1);
    const opts = {
        method:'GET',
        xhrFields: {withCredentials: true},
        crossDomain: true,
        // dataType: 'json',
        // contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
    }
    return $.ajax(`${host}${u}?${data}`,opts);
};

export default {
    get,
    post
}